namespace Resharper.Abstractions.CodeDom.Expressions.UnaryOperatorExpressions
{
  using JetBrains.Annotations;
  using JetBrains.ReSharper.Psi.CSharp.Tree;

  /// <summary>Defines the <see cref="UnaryOperatorExpression"/> class.</summary>
  public class UnaryOperatorExpression : Expression
  {
    #region Constructors and Destructors

    /// <summary>Initializes a new instance of the <see cref="UnaryOperatorExpression"/> class.</summary>
    /// <param name="unaryOperatorExpression">The unary operator expression.</param>
    internal UnaryOperatorExpression([NotNull] IUnaryOperatorExpression unaryOperatorExpression) : base(unaryOperatorExpression)
    {
    }

    #endregion

    #region Properties

    /// <summary>
    /// Gets the left operand.
    /// </summary>
    [CanBeNull]
    public Expression Operand
    {
      get
      {
        var expression = this.InternalUnaryOperatorExpression.Operand;

        return expression != null ? ExpressionFactory.GetExpression(expression) : null;
      }
    }

    /// <summary>
    /// Gets the operator sign.
    /// </summary>
    [NotNull]
    public string OperatorSign
    {
      get
      {
        var token = this.InternalUnaryOperatorExpression.OperatorSign;

        return token != null ? token.GetText() : string.Empty;
      }
    }

    /// <summary>The literal expression field.</summary>
    [NotNull]
    internal IUnaryOperatorExpression InternalUnaryOperatorExpression
    {
      get
      {
        return (IUnaryOperatorExpression)this.InternalExpression;
      }
    }

    #endregion

    #region Public Methods

    /// <summary>
    /// Gets the operator sign text range.
    /// </summary>
    public bool OperatorSignContains(int offset)
    {
      var operatorSign = this.InternalUnaryOperatorExpression.OperatorSign;
      if (operatorSign == null)
      {
        return false;
      }

      var range = operatorSign.GetNavigationRange();

      return range.TextRange.Contains(offset);
    }

    #endregion
  }
}